﻿using System;
using System.Text.Json;
using Adaptive.Agrona;
using Unity.Type;
using Unity.Type.BinaryType;

namespace Unity.Commons.Client.Offline
{
    public class ConsoleJsonHandler : IOfflineHandler
    {
        private static readonly MetadataEDeserializer Deserializer
            = new MetadataEDeserializer();

        private static readonly JsonSerializerOptions SerializerOptions
            = new JsonSerializerOptions();

        public ConsoleJsonHandler()
        {
            // configure serializerOptions
            SerializerOptions.WriteIndented = false;
        }

        public void onAction(IDirectBuffer buffer, int offset, int length)
        {
            byte[] serializedObject = new byte[length];
            buffer.GetBytes(offset, serializedObject, 0, length);
            MetadataE metadataObject = Deserializer.Deserialize(serializedObject);
            string jsonObject = JsonSerializer.Serialize(metadataObject, SerializerOptions);
            Console.WriteLine(jsonObject);
        }
    }
}